# Maintainer: Kurt Marasco <celilo@lavabit.com>
# Contributor: Pascal Ernster <aur at hardfalcon dot net>
# Contributor: Stuart Cardall <developer at it-offshore dot co.uk>
pkgname=hiawatha
pkgver=11.5
pkgrel=0
_php=php81
pkgdesc='Secure and advanced webserver'
url='https://hiawatha.leisink.net/'
arch="all"
license='GPL-2.0-only'
options="suid !check"
subpackages="$pkgname-doc $pkgname-openrc $pkgname-letsencrypt::noarch"
makedepends="cmake libxml2-dev libxslt-dev mbedtls-dev"
source="https://hiawatha.leisink.net/files/hiawatha-$pkgver.tar.gz
	hiawatha.initd
	hiawatha.conf.sample
	letsencrypt.helper
	"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=None \
		-DUSE_SYSTEM_MBEDTLS=ON \
		$CMAKE_CROSSOPTS .
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --build build --target install

	sed -i 's|#ServerId = www-data|ServerId = nobody|' \
		"$pkgdir"/etc/hiawatha/hiawatha.conf
	sed -i 's|www-data|nobody|g' build/logrotate.d/hiawatha
	install -Dm644 build/logrotate.d/hiawatha \
		"$pkgdir"/etc/logrotate.d/hiawatha
	install -Dm755 "$srcdir"/hiawatha.initd \
		"$pkgdir"/etc/init.d/hiawatha
	install -Dm644 "$srcdir"/hiawatha.conf.sample \
		"$pkgdir"/usr/share/doc/hiawatha/hiawatha.conf.sample
}

letsencrypt() {
	depends="$_php $_php-openssl"
	pkgdesc="PHP scripts to generate SSL certificates with letsencrypt"

	mkdir -p "$subpkgdir"/usr/sbin "$subpkgdir"/usr/share/man/man1 "$subpkgdir"/usr/lib/hiawatha
	mv "$pkgdir"-doc/usr/share/man/man1/lefh.1.gz "$subpkgdir"/usr/share/man/man1/
	mv "$pkgdir"/usr/lib/hiawatha/letsencrypt "$subpkgdir"/usr/lib/hiawatha
	rmdir -p "$pkgdir"/usr/lib/hiawatha || true
	mv "$pkgdir"/usr/sbin/lefh "$subpkgdir"/usr/sbin/
	mkdir -p "$subpkgdir"/usr/lib/hiawatha/letsencrypt/scripts
	install -Dm750 "$builddir"/extra/letsencrypt/scripts/* "$subpkgdir"/usr/lib/hiawatha/letsencrypt/scripts/
	install -Dm750 "$srcdir"/letsencrypt.helper "$subpkgdir"/usr/sbin/letsencrypt
}

sha512sums="
575013567488b76bf37734a882e6608378a236f4f8cce7bb35a3949c97b894c1efa5963ab589f59fe27e6e1ba06ff0490f0c7bf7cfc87cd77bd5b60ca85a4bb6  hiawatha-11.5.tar.gz
cec0adfb48e1751cf13031f2c28f4590340b2c747aa8e81d18c16bc8789bce69b2d9d99290a93b74b501db3b63190a071033a78abeeb8f60becba2dcaf3b5c40  hiawatha.initd
771bed17b5831bb9d4a28eb94482dd39de8c4b5b35b2c08e1c303a2778254f9e34e40a8d791cc7ef4e73740f474c502685252378e7fe2cba494c782c11f960d4  hiawatha.conf.sample
dbdd0cdf975b226d2ac27257861a1cc6c1f77c8387d632014368a1c835817a33b780f446cd3fdb7d9ca4322eaede1fea28652dde70928cc8a73f0b05dba46f6e  letsencrypt.helper
"
